package com.laitaoo.views.home
{
	import com.laitaoo.event.HomeEvent;
	import com.laitaoo.event.KindsEvent;
	import com.laitaoo.views.BaseMediator;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.utils.ObjectUtil;

	[Component]
	public class HomeMediator extends BaseMediator
	{
		public function HomeMediator()
		{
		}
		
		public var homeView:HomeView;
		
		/* tree 数据源 */
		[Bindable]
		public var treeData:Object;
		
		[Bindable]
		public var productList:ArrayCollection;
		
		/** 上货列表 */
		[Bindable]
		public var stockList:ArrayCollection;
		
		[Bindable]
		public var productKinds:ArrayCollection;
		
		/** 总成本 */
		[Bindable]
		public var totalCost:String;
		
		/** 总销售额 */
		[Bindable]
		public var totalTunover:String;
		
		/** 总利润 */
		[Bindable]
		public var totalProfit:String;
		
		/**
		 * 饼图例如百分比数据 
		 **/
		[Bindable]
		public var profitList:ArrayCollection = new ArrayCollection();
		
		[Bindable]
		public var costList:ArrayCollection = new ArrayCollection();
		
		[Bindable]
		public var tunOverList:ArrayCollection = new ArrayCollection();
		
		private var _dayId:int = -1;
		
		private var _mouthXml:XML = null;
		
		[EventHandler]
		public function treeYearResultHandler(event:HomeEvent):void{
			
			treeData = event.years;
		}
		
		/**
		 * 添加新的年份
		 * 
		 * @param year:String --- 具体的年份 
		 **/
		public function addYear(year:String):void{
			
			var homeEvent:HomeEvent = new HomeEvent(HomeEvent.ADD_MOUTH);
			homeEvent.year = year;
			eventBus.dispatchEvent(homeEvent);
		}
		
		// ----------------------------------------------------------------------------------------
		
		/**
		 * 获取每天的商品信息
		 * 
		 * @param dayId:int --- 每天日期ID
		 **/
		public function getProductList(dayId:int):void{
			
			_dayId = dayId;
			_mouthXml = null;
			// 加载商品种类
			var homeEvent:HomeEvent = new HomeEvent(HomeEvent.PRODUCTLIST);
			homeEvent.dayId = dayId;
			eventBus.dispatchEvent(homeEvent);	
		}
		/** 
		 * 添加新商品类型
		 * 
		 * @param productName:String --- 产品名称
		 * @param kindId:int --- 种类ID
		 **/
		public function addProduct(productName:String,kindId:int):void{
			
			var homeEvent:HomeEvent = new HomeEvent(HomeEvent.ADDPRODUCT);
			homeEvent.productName = productName;
			homeEvent.kindId = kindId;
			eventBus.dispatchEvent(homeEvent);
		}
		
		/**
		 * 添加商品成功后，刷新商品列表
		 **/
		[EventHandler]
		public function addProductResultHandler(event:HomeEvent):void{
			
			var homeEvent:HomeEvent = new HomeEvent(HomeEvent.PRODUCTLIST);
			homeEvent.dayId = _dayId;
			eventBus.dispatchEvent(homeEvent);		
		}
		/**
		 * 获取种类列表返回结果 
		 **/
		[EventHandler]
		public function productListResultHandler(event:HomeEvent):void{
			
			productList = event.produtList;
			
			var tempKinds:ArrayCollection = ObjectUtil.copy(productKinds) as ArrayCollection;
			
			for each (var kindObj:Object in tempKinds) 
			{
				kindObj.kinds = [];
				
				for each (var productObj:Object in productList) 
				{
					if(kindObj.Id == productObj.kindId){
						productObj.kindName = kindObj.kindName;
						(kindObj.kinds as Array).push(productObj);
					}
				}
			}
			homeView.hd.source = tempKinds;
			// === 图标信息数据 ===
			// 利润
			profitList.removeAll();
			for each (var obj:Object in productList) 
			{
				var profitObj:Object = new Object();
				profitObj.productName = obj.productName;
				profitObj.value = obj.profit;
				
				profitList.addItem(profitObj);
			}
			
			// 成本
			costList.removeAll();
			for each (var obj1:Object in productList) 
			{
				var costObj:Object = new Object();
				costObj.productName = obj1.productName;
				costObj.value = obj1.cost;
				
				costList.addItem(costObj);
			}
			
			// 营业额
			tunOverList.removeAll();
			for each (var obj2:Object in productList) 
			{
				var tunOverObj:Object = new Object();
				tunOverObj.productName = obj2.productName;
				tunOverObj.value = obj2.tunover;
				
				tunOverList.addItem(tunOverObj);
			}
		}
		/**
		 * 保存修改或添加的商品
		 * 
		 * @param  list:ArrayCollection --- 修改或添加集合
		 **/
		public function saveProduct(list:ArrayCollection):void{
			
			var homeEvent:HomeEvent = new HomeEvent(HomeEvent.SAVEPRODUCTBYDAYID);
			homeEvent.produtList = list;
			homeEvent.dayId = _dayId;
			eventBus.dispatchEvent(homeEvent);
		}
		/**
		 * 修改商品名称 
		 * 
		 * @param productName:String --- 商品名称
		 * @param productId:int --- 商品ID
		 **/
		public function saveProductName(productName:String,productId:int):void{
			
			var homeEvent:HomeEvent = new HomeEvent(HomeEvent.SAVEPRODUCTNAME);
			homeEvent.productName = productName;
			homeEvent.productId = productId;
			eventBus.dispatchEvent(homeEvent);
		}
		/**
		 * 修改每天的汇总信息
		 * 
		 * @param totalCost:int --- 总成本
		 * @param totalTunover:int --- 总营业额
		 * @param totalProfit:int --- 总利润
		 **/
		public function updateProductTotalByDayId(totalCost:int,totalTunover:int,totalProfit:int):void{
			
			var homeEvent:HomeEvent = new HomeEvent(HomeEvent.UPDATETOTALINFO);
			homeEvent.totalCost = totalCost;
			homeEvent.totalProfit = totalProfit;
			homeEvent.totalTunover = totalTunover;
			homeEvent.dayId = _dayId;
			eventBus.dispatchEvent(homeEvent);
		}
		/**
		 * 获取每天的汇总信息 
		 * 
		 * @param dayId:int --- 日期ID
		 **/
		public function getTotalByDayId(dayId:int):void{
			
			var homeEvent:HomeEvent = new HomeEvent(HomeEvent.GETTOTALINFOBYDAYID);
			homeEvent.dayId = dayId;
			eventBus.dispatchEvent(homeEvent);
		}
		
		[EventHandler]
		public function getTotalInfoByDayIdResult(event:HomeEvent):void{
			
			totalCost = event.totalCost.toString();
			totalTunover = event.totalTunover.toString();
			totalProfit = event.totalProfit.toString();
		}
		/** 
		 *获取一个月的统计信息
		 * 
		 * @param 当前月节点的信息
		 **/
		public function getMouthChart(mouthXml:XML):void{
			
			_dayId = -1;
			_mouthXml = mouthXml;
			
			var homeEvent:HomeEvent = new HomeEvent(HomeEvent.GETMOUTHCHART);
			homeEvent.mouthXml = mouthXml;
			eventBus.dispatchEvent(homeEvent);
		}
		
		[EventHandler]
		public function getProductKindsResultHandler(event:HomeEvent):void{
			productKinds = event.productKinds;
			
			if(_dayId != -1){
				getProductList(_dayId);
			}else if(_mouthXml){
				getMouthChart(_mouthXml);
			}
		}
			
		// 编辑分类或新增分类后刷新相关数据
		[EventHandler]
		public function updateKindResultHandler(event:KindsEvent):void{
			
			// 刷新分类列表
			var homeEvent1:HomeEvent = new HomeEvent(HomeEvent.GETPRODUCTKINDS);
			eventBus.dispatchEvent(homeEvent1);
		}
		/**
		 * 获取商品的单价
		 * 
		 * @param productId:String --- 商品ID
		 * */
		public function getProductPrice(productId:String):void{
			
			var homeEvent:HomeEvent = new HomeEvent(HomeEvent.productPrice);
			homeEvent.productId = int(productId);
			eventBus.dispatchEvent(homeEvent);
		}
	}
}




